# coding: utf-8
from flask import render_template, request
from flask_login import login_required

from app.products.models import Order
from app.products.order_forms import OrderSearchForm
from . import product_bp


@product_bp.route('/orders', methods=['GET', 'POST'])
@login_required
def order_list():
    """
    create by ld_wu 2018-01-02
    订单列表
    :return:
    """
    search_form = OrderSearchForm(formdata=request.form or request.args)

    search_args = {
        key: value for key, value in search_form.data.items() if value
    }

    brand_query = Order.generate_list_query_condition(search_args)

    pagination = brand_query.paginate(error_out=False)
    orders = pagination.items

    return render_template(
        'order/order_list.html',
        search_form=search_form,
        orders=orders,
        pagination=pagination,
        url_kwargs=search_args
    )